package com.whx.authorization.util;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;

/**
 * 类名：SimpleHttpUtil
 * 类路径： com.whx.authorization.util.SimpleHttpUtil
 * 创建者： whx
 * 创建时间： 10/8/18 5:43 PM
 * 项目： authorization_center
 * 描述：
 */
public class SimpleHttpUtil {

    public static Connection.Response execute(String url, Map<String, String> params, Map<String, String> headers
            , Map<String, String> cookies, Connection.Method method) throws IOException {
        Connection connect = Jsoup.connect(url);
        if (cookies != null && !cookies.isEmpty()) {
            connect.cookies(cookies);
        }
        if (params != null && !params.isEmpty()) {
            connect.data(params);
        }
        if (headers != null && !headers.isEmpty()) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                connect.header(header.getKey(), header.getValue());
            }
        }
        if (method == null) {
            method = Connection.Method.GET;
        }
        connect.method(method);
        return connect.execute();
    }
}
